<?php

namespace addons\qingdongams\controller;

use addons\qingdongams\model\Contacts;
use app\admin\model\Admin;
use app\common\library\Sms;
use app\common\library\Token;
use fast\Random;
use think\Validate;

/**
 * 用户中心
 */
class WebPerson extends WebIndexApi
{
    protected $layout = 'default';
    protected $noNeedLogin = ['login', 'register', 'third','logintest'];
    protected $noNeedRight = ['*'];
    /**
     * @var \addons\qingdongams\model\Person
     */
    protected $model;


    public function __construct()
    {
        parent::__construct();
        $this->model = new \addons\qingdongams\model\Person();

    }

    /**
     * 会员登录
     */
    public function login()
    {
        if ($this->request->isPost()) {
            $account = $this->request->post('account');
            $code=$this->request->post('code');
            $rule = [
                'account' => 'require|length:3,50',
            ];
            $msg = [
                'account.require' => '账户不能为空',
                'account.length' => '账户必须3-50个字符',
            ];
            $data = [
                'account' => $account,
            ];
            $validate = new Validate($rule, $msg);
            $result = $validate->check($data);
            if (!$result) {
                $this->error(__($validate->getError()));
            }
            if (!Sms::check($account, $code, 'mobilelogin')) {
                $this->error(__('验证码不正确'));
            }

            $info = $this->model->where(['account' => $account])->find();
            if (!$info) {
                $data = [
                    'nickname' => '',
                    'password' => '',
                    'email' => '',
                    'account' => $account];
                $data['salt'] = Random::alnum();
                $data['password'] = md5(md5($data['password']) . $data['salt']);
                if ($customerId = Contacts::where(['mobile' => $data['account']])->value('customer_id')) {
                    $data['customer_id'] = $customerId;
                } else {
                    $this->error('账号不存在');
                }
                $this->model->allowField(true)->save($data);
                $info = $this->model->where(['account' => $account])->find();
            }
            $token = md5('person'.$info['id'] . rand(1000, 9999));
            if (Token::set($token,$info['id'], $this->keeptime)) {
                $this->success(__('登录成功'), ['token' => $token]);
            }
            $this->error('登录失败');
        }
    }

    /**
     * 退出登录
     */
    public function logout()
    {
        $token = input('token');
        //删除Token
        Token::delete($token);
        $this->success(__('Logout successful'));
    }


    // 企业信息
    public function companyInfo(){
        $info = Admin::get(1);
        $info->avatar = cdnurl($info->avatar,true);
        $this->success('',$info);
    }
//直接体验
    public function logintest()
    {
        $info = $this->model->where([])->order('id asc')->find();
        if (!$info) {
            $contacts = Contacts::where([])->order('id asc')->find();
            $data = [
                'nickname' => '',
                'password' => '',
                'email' => '',
                'account' => $contacts['mobile']];
            $data['salt'] = Random::alnum();
            $data['password'] = md5(md5($data['password']) . $data['salt']);
            $data['customer_id'] = $contacts['customer_id'];
            $this->model->allowField(true)->save($data);
            $info = $this->model->where(['account' => $contacts['mobile']])->find();
        }
        $token = md5('person'.$info['id'] . rand(1000, 9999));
        if (Token::set($token,$info['id'], $this->keeptime)) {
            $this->success(__('登录成功'), ['token' => $token]);
        }
        $this->error('登录失败');

    }
}
